#include "../afw/afwApp.h"

#include <GL/gl.h>
#include <GL/glut.h>

using namespace one_rag::App;

typedef GlutApplication App;


class MyGlutWindow : public GlutWindowDelegate
{
  App::GLWindow _wnd;
    float theta;
public:
    void CreateWindow()
    {
      glutInitDisplayMode(GLUT_RGBA);
      _wnd.Create("Tut2-OpenGL",windowPosDefault,352*16/9,352);
      _wnd.SetDelegate(this);
    }

    void NextFrame()
    {
      theta += 1.0f;
    }

    void Update()
    {
      _wnd.PostRedisplay();

    }

  void OnDisplay()
  {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();
    glRotatef(theta, 0.0f, 0.0f, 1.0f);

    glBegin(GL_TRIANGLES);

        glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.0f,   1.0f);
        glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(0.87f,  -0.5f);
        glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(-0.87f, -0.5f);

    glEnd();

    glPopMatrix();

    glFlush();
  }

};

class MyGlutApplication : public GlutAppDelegate
{
  App theApp;
  MyGlutWindow glutWnd;

  void OnTimer(int id){
    glutWnd.NextFrame();
    glutWnd.Update();
    // Glut timers fire only once. So queue another one!
    theApp.SetTimer(33,101);
  }

public:
  int Run(int argc, char* argv[])
  {
    theApp.Init(&argc,argv);
    theApp.SetDelegate(this);
    theApp.SetTimer(33,101);

    glutWnd.CreateWindow();

    theApp.Run();

    return 0;
  }

};

int main(int argc, char* argv[])
{
  MyGlutApplication myApp;
  return myApp.Run(argc,argv);
}
